package com.mediacloud.app.newsmodule.fragment.recommend;


import android.content.Context;
import android.graphics.Rect;
import android.util.Log;

import com.eventTrack.CDPTrackReportManager;
import com.mediacloud.app.assembly.util.Utility;
import com.mediacloud.app.model.component.ComponentItem;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.ArticleListData;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.adaptor.component.ComponentListViewX;
import com.mediacloud.app.newsmodule.adaptor.component.CustomLinearLayout;
import com.mediacloud.app.newsmodule.adaptor.component.IExposure;
import com.mediacloud.app.newsmodule.utils.NotifyEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yoake on 2017/9/20.
 */

public class ComponentRelatedNewsView extends BaseComponentRelatedView /*implements IExposure*/ {

    private ComponentListViewX container;
    private List<ArticleItem> listData;
    CatalogItem cat = new CatalogItem();

    public ComponentRelatedNewsView(Context context) {
        super(context);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Override
    public int setLayoutRes() {
        return R.layout.component_related_recommendation_new;
    }

    @Override
    public void initOtherViews() {
        container = Utility.findViewById(mRootView, R.id.container);
        container.setDetailComponentList(true);
    }

    @Override
    public void onDataSuccess(ArticleListData data) {
        List<ArticleItem> listArticles = data.articleList;
        if (listArticles == null || listArticles.size() == 0) {
            return;
        }
        mRootView.setVisibility(VISIBLE);
        this.setVisibility(VISIBLE);

        this.listData = listArticles;
        container.setData(listArticles, cat,"相关新闻");
        for (int i = 0; i < listArticles.size(); i++) {
            CDPTrackReportManager.getInstance().NewsRPExpose(i,listArticles.get(i),"相关新闻");
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onNotifyEvent(NotifyEvent event) {
        if (listData != null) {
            container.setData(listData, cat,"相关新闻");
        }
    }

    @Override
    public void onDataFault(Object data) {

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

//    @Override
//    public List<ArticleItem> getExposureArticles() {
//        try {
//            if (container.getContainer() != null) {
//                int childCount = container.getContainer().getChildCount();
//                ArrayList<ArticleItem> sendData = new ArrayList<>();
//                for (int i = 0; i < childCount; i++) {
//                    if (container.getContainer().getChildAt(i).getGlobalVisibleRect(new Rect())) {
//                        sendData.add(listData.get(i));
//                    }
//                }
//                return sendData;
//            }
//        } catch (Exception e) {
//
//        }


//        return listData;
//    }

//    @Override
//    public ComponentItem getExposureComponentItem() {
//        return null;
//    }
}